//
//  ImageToSoundViewController.m
//  ImageToSound
//
//  Created by jvmilazz on 9/26/11.
//  Copyright 2011 ASU. All rights reserved.
//

#import "ImageToSoundViewController.h"

@implementation ImageToSoundViewController

@synthesize selectedImage;
@synthesize audioManager;
@synthesize statusLabel;
@synthesize playButton;
@synthesize recordButton;
@synthesize createSoundAlert;


- (void) dealloc
{
    
    CFRelease(audioManager.fileURL);
    [super dealloc];
}

- (void) didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void) viewDidLoad
{
    // Get audio file page
    char path[256];
    //[self getFilename:path maxLenth:sizeof path];
    [audioManager getFilename:path maxLenth:sizeof path];
    audioManager.fileURL = CFURLCreateFromFileSystemRepresentation(NULL, (UInt8*)path, strlen(path), false);
    
    // Init state variables
    PlayState n = audioManager.playState;
    n.playing = false;
    audioManager.playState = n;
        
    RecordState na = audioManager.recordState;
    na.recording = false;
    audioManager.recordState = na;
    
    [super viewDidLoad];
}


- (void) viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(IBAction) buttonClicked_selectImage
{
    
    picker = [[UIImagePickerController alloc] init];
    
    picker.delegate = self;
    
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])  
    {
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    } else
    {
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    }
    
    [self presentModalViewController:picker animated:YES];
}



- (void) imagePickerControllerDidCancel:(UIImagePickerController *) Picker {
    [[Picker parentViewController] dismissModalViewControllerAnimated:YES];
    [Picker release];
}


- (void) imagePickerController:(UIImagePickerController *) Picker
didFinishPickingMediaWithInfo:(NSDictionary *)info 
{
    selectedImage.image = [info objectForKey:UIImagePickerControllerOriginalImage];
    
    CGImageRef cgImage = [selectedImage.image CGImage];
    CGDataProviderRef provider = CGImageGetDataProvider(cgImage);
    CFDataRef bitmapData = CGDataProviderCopyData(provider);
    
    
    imageSize = selectedImage.image.size;
    imageData = (RGBPixel*)CFDataGetBytePtr(bitmapData);
    
    [[Picker parentViewController] dismissModalViewControllerAnimated:YES];
    [Picker release];
}


-(IBAction) buttonClicked_createSound
{
    //Create an alert view
    createSoundAlert = [[UIAlertView alloc] initWithTitle:@"Sound File" message:@"Creating a sound file..." delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
    [createSoundAlert show];
    
    // Inside the alert view create an activity indicator view for the duration of creating the sound file
    if(createSoundAlert != nil)
    {
        UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
        
        indicator.center = CGPointMake(createSoundAlert.bounds.size.width/2, createSoundAlert.bounds.size.height-45);
        [indicator startAnimating];
        [createSoundAlert addSubview:indicator];
        [indicator release];
    }
    
    // Call a function that actually converts the image into a sound file
    // TODO: (make Adam do this)
    // JVM: The steps necessary are: create Histogram from image, run through mapping, call createWAVFile()
    
    //audioManager.generateWAVFile(@"debugSound");
    //selectedImage.image.
    
    // Dismiss createSoundAlert after the file is created.
    [createSoundAlert dismissWithClickedButtonIndex:0 animated:YES];
    [createSoundAlert release];
}


- (RGBPixel*)pixelDataForRow:(int)row column:(int)column  
{
    
    //  Return a pointer to the pixel data
    
    return &imageData[ row * column ];      
    
}

@end
